//
//  YAWelcomeController.swift
//  KeepCliming
//
//  Created by 鱼龙之梦 on 15/9/6.
//  Copyright © 2015年 panyanzhi. All rights reserved.
//

import UIKit
import SDWebImage
class YAWelcomeController: UIViewController {
    //MARK:---头像底部约束
    private var iconBottomCons:NSLayoutConstraint?
    //MARK:---懒加载
    private lazy var backImageView:UIImageView = UIImageView(image:UIImage(named: "ad_background"))
    private lazy var iconView:UIImageView = {
        let iv = UIImageView(image:UIImage(named: "avatar_default_big"))
        iv.layer.cornerRadius = 45 //注意：只要超过半径的一半都会变成圆
        iv.layer.masksToBounds = true
        return iv
    }()
    private lazy var label:UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.textColor = UIColor.grayColor()
        return label
    }()
    //MARK:--- 设置各自控件 约束
    private func setupUIConstraints(){
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        label.translatesAutoresizingMaskIntoConstraints = false
        //MARK:---布局背景图片
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backImageView]))
        //MARK:---约束头像图片
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -200))
        self.iconBottomCons = view.constraints.last;
        //MARK:---头像的宽高约束
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        //MARK:---欢迎归来的 文本
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        setupUIConstraints()
        //MARK:---设置用户头像
        let url = YAUserAcountVM.sharedUserAccount.avatarURL!
        if url.absoluteString != ""{
          iconView.sd_setImageWithURL(YAUserAcountVM.sharedUserAccount.avatarURL)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK:---在视图开始显示的时候，设置动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        let h = -(UIScreen.mainScreen().bounds.height + iconBottomCons!.constant)
        iconBottomCons?.constant = h
        label.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.label.alpha = 1
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(YARootViewControllerSwitchNotification, object: nil)
                })
        }    } 
}
